package com.zjdiepu.www.manage.view;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.enums.status.DpStatus;
import com.zjdiepu.www.base.enums.status.LoanEditStatus;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.ModeIDType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.commpent.RedisObjectClient;
import com.zjdiepu.www.commpent.ViewScopeControllerHelper;
import com.zjdiepu.www.commpent.mapper.other.OtherAuditInfoVoMapper;
import com.zjdiepu.www.commpent.mapper.yw.AbstractYwBusinessLoanVoNewMapper;
import com.zjdiepu.www.ibiz.base.IDpBaseDataDictionaryService;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.cw.ICwIncomeExpensesItemService;
import com.zjdiepu.www.ibiz.dh.IDhLoanAfterService;
import com.zjdiepu.www.ibiz.his.IHisYwBusinessLoanService;
import com.zjdiepu.www.ibiz.other.IOtherAuditInfoService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessExtrasService;
import com.zjdiepu.www.ibiz.yw.IYwBusinessLoanEditService;
import com.zjdiepu.www.ibiz.yw.IYwFeesSubService;
import com.zjdiepu.www.model.base.dto.DpBaseDataDictionary;
import com.zjdiepu.www.model.base.ext.DpBaseDataDictionaryExt;
import com.zjdiepu.www.model.cw.response.CwFundItem;
import com.zjdiepu.www.model.his.dto.HisYwBusinessLoan;
import com.zjdiepu.www.model.other.dto.OtherAuditInfo;
import com.zjdiepu.www.model.other.ext.OtherAuditInfoExt;
import com.zjdiepu.www.model.yw.dto.YwBusinessCar;
import com.zjdiepu.www.model.yw.dto.YwBusinessLoanEdit;
import com.zjdiepu.www.model.yw.response.YwBusinessDetail;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 修改贷款信息-审核1Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月10日 14时56分<br/>
 * @since 1.0<br/>
 */
@Controller
@RequestMapping(value= "HisYwBusinessLoanViewController", produces="text/html;charset=UTF-8")
@Api(tags = {"贷后信息登记"}, produces="text/html;charset=UTF-8")
public class HisYwBusinessLoanViewController extends BaseController {
    @Resource
    private IDhLoanAfterService dhLoanAfterService;
    @Resource
    private ViewScopeControllerHelper viewScopeControllerHelper;
    @Resource
    private ICachedDictionaryService cachedDictionaryService;
    @Resource
    private IYwBusinessLoanEditService ywBusinessLoanEditService;
    @Resource
    private IHisYwBusinessLoanService hisYwBusinessLoanService;
    @Resource
    private AbstractYwBusinessLoanVoNewMapper hisYwBusinessLoanVoMapper;
    @Resource
    private ICwIncomeExpensesItemService cwIncomeExpensesItemService;
    @Resource
    private IDpBaseDataDictionaryService dBaseDataDictionaryService;
    @Resource
    private IYwFeesSubService ywFeesSubService;
    @Resource
    private IYwBusinessExtrasService ywBusinessExtrasService;
    @Resource
    private IOtherAuditInfoService otherAuditInfoService;
    @Resource
    private OtherAuditInfoVoMapper otherAuditInfoVoMapper;
    @Resource
    private RedisObjectClient redisObjectClient;
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: changeLoanShDetail  <br/>
     * @Description: 修改贷款信息-审核1详情 <br/>
     * @param model
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改贷款信息-审核1详情", response = Boolean.class, httpMethod="GET")
    @RequestMapping(value = "changeLoanShDetail", method = RequestMethod.GET)
    public String changeLoanShDetail(Model model, String id) {
    	if (StringUtils.isBlank(id)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(id, model);
    	YwBusinessCar car = viewScopeControllerHelper.preparedCar(id,ywBusinessDetail.getCustomerId(), model);
    	if(null != car) {
    		YwBusinessLoanEdit ywBusinessLoanEdit = new YwBusinessLoanEdit();
        	ywBusinessLoanEdit.setDpStatus(DpStatus.NORMAL.getCode());
        	ywBusinessLoanEdit.setLoanEditStatus(LoanEditStatus.YI_SHEN_QING.getCode());
        	ywBusinessLoanEdit.setBusiId(id);
        	ywBusinessLoanEdit = ywBusinessLoanEditService.selectRefined(ywBusinessLoanEdit);
        	if(null != ywBusinessLoanEdit) {
        		hisYwBusinessLoanVoMapper.mapper(ywBusinessLoanEdit);
        		ywBusinessLoanEdit.setCarType(car.getCarType());
        		model.addAttribute("ywBusinessLoanEdit", ywBusinessLoanEdit);
        		
        		String hisLoanId = ywBusinessLoanEdit.getHisLoanId();
        		if(StringUtils.isNotBlank(hisLoanId)) {
        			HisYwBusinessLoan hisYwBusinessLoan = hisYwBusinessLoanService.selectById(hisLoanId);
        			hisYwBusinessLoan.setCarType(car.getCarType());
        			hisYwBusinessLoanVoMapper.mapper(hisYwBusinessLoan);
        			model.addAttribute("hisYwBusinessLoan", hisYwBusinessLoan);
        		}
        	}
    	}
    	viewScopeControllerHelper.preparedYwFee(id, ywBusinessDetail.getCustomerId(), model);
    	
    	return "system/cw/shUpdatedaikuanDetail";
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: hisChangeLoanShDetail  <br/>
     * @Description: 贷款信息修改历史记录 <br/>
     * @param model
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "贷款信息修改历史记录", response = Boolean.class, httpMethod="GET")
    @RequestMapping(value = "hisChangeLoanShDetail", method = RequestMethod.GET)
    public String hisChangeLoanShDetail(Model model, String id, String hisEditLoanId) {
    	if (!StringUtils.isNoneBlank(id, hisEditLoanId)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(id, model);
    	viewScopeControllerHelper.preparedCar(id,ywBusinessDetail.getCustomerId(), model);
    	
    	YwBusinessLoanEdit ywBusinessLoanEdit = ywBusinessLoanEditService.selectById(hisEditLoanId);
    	
    	if(null == ywBusinessLoanEdit) {
    		ywBusinessLoanEdit = new YwBusinessLoanEdit();
        	ywBusinessLoanEdit.setDpStatus(DpStatus.NORMAL.getCode());
//        	ywBusinessLoanEdit.setLoanEditStatus(LoanEditStatus.YI_SHEN_QING.getCode());
        	ywBusinessLoanEdit.setBusiId(id);
        	ywBusinessLoanEdit = ywBusinessLoanEditService.selectRefined(ywBusinessLoanEdit);
    	}
    	
    	if(null != ywBusinessLoanEdit) {
    		hisYwBusinessLoanVoMapper.mapper(ywBusinessLoanEdit);
    		model.addAttribute("ywBusinessLoanEdit", ywBusinessLoanEdit);
    		
    		String hisLoanId = ywBusinessLoanEdit.getHisLoanId();
    		if(StringUtils.isNotBlank(hisLoanId)) {
    			HisYwBusinessLoan hisYwBusinessLoan = hisYwBusinessLoanService.selectById(hisLoanId);
    			hisYwBusinessLoanVoMapper.mapper(hisYwBusinessLoan);
    			model.addAttribute("hisYwBusinessLoan", hisYwBusinessLoan);
    		}
    	}
    	
    	viewScopeControllerHelper.preparedYwFee(id, ywBusinessDetail.getCustomerId(), model);
    	
    	OtherAuditInfoExt otherAuditInfoExt = new OtherAuditInfoExt();
    	otherAuditInfoExt.withOutPage();
    	otherAuditInfoExt.setBaseWhere();
    	otherAuditInfoExt.setBusiId(id);
    	otherAuditInfoExt.setWhereSql(" AND `MODE_ID` IN ('" + ModeIDType.XGDKSHY.getCode() +"','" + ModeIDType.XGDKSHE.getCode() + "')");
    	otherAuditInfoExt.setOrderSql(" `CREATED` DESC");
    	List<OtherAuditInfo> otherAuditInfos = otherAuditInfoService.selectByExt(otherAuditInfoExt);
    	otherAuditInfoVoMapper.mapper(otherAuditInfos);
    	model.addAttribute("otherAuditInfos", otherAuditInfos);
    	
    	return "system/cw/his_shUpdatedaikuanDetail";
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: changeLoanShBDetail  <br/>
     * @Description: 修改贷款信息-审核2详情 <br/>
     * @param model
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改贷款信息-审核2详情", response = Boolean.class, httpMethod="GET")
    @RequestMapping(value = "changeLoanShBDetail", method = RequestMethod.GET)
    public String changeLoanShBDetail(Model model, String id) {
    	if (StringUtils.isBlank(id)) {
    		ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
    	}
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(id, model);
    	if(null != ywBusinessDetail) {
    		YwBusinessCar car = viewScopeControllerHelper.preparedCar(id,ywBusinessDetail.getCustomerId(), model);
    		if(null != car) {
    			viewScopeControllerHelper.preparedLoan(id, car.getId(), model, car.getCarType());

    	    	YwBusinessLoanEdit ywBusinessLoanEdit = new YwBusinessLoanEdit();
    	    	ywBusinessLoanEdit.setDpStatus(DpStatus.NORMAL.getCode());
    	    	ywBusinessLoanEdit.setLoanEditStatus(LoanEditStatus.SHEN_HE_2_SHEN_HE_ZHONG.getCode());
    	    	ywBusinessLoanEdit.setBusiId(id);
    	    	ywBusinessLoanEdit = ywBusinessLoanEditService.selectRefined(ywBusinessLoanEdit);
    	    	if(null != ywBusinessLoanEdit) {
//    	    		直接取值
    	    		hisYwBusinessLoanVoMapper.mapper(ywBusinessLoanEdit); 
    	    		ywBusinessLoanEdit.setCarType(car.getCarType());
    	    		model.addAttribute("ywBusinessLoanEdit", ywBusinessLoanEdit);
    	    		
    	    		String hisLoanId = ywBusinessLoanEdit.getHisLoanId();
    	    		if(StringUtils.isNotBlank(hisLoanId)) {
    	    			HisYwBusinessLoan hisYwBusinessLoan = hisYwBusinessLoanService.selectById(hisLoanId);
    	    			if(null != hisYwBusinessLoan) {
//    	        			直接取值
    	    				hisYwBusinessLoanVoMapper.mapper(hisYwBusinessLoan);
    	    				hisYwBusinessLoan.setCarType(car.getCarType());
    	    				model.addAttribute("hisYwBusinessLoan", hisYwBusinessLoan);
    	    				
    	    			}
    	    		}
    	    	}
    		}
    		viewScopeControllerHelper.preparedYwFee(id, ywBusinessDetail.getCustomerId(), model);
    	}
    	
//    	获取费用数据字典=====================
    	DpBaseDataDictionaryExt dpBaseDataDictionaryExt = new DpBaseDataDictionaryExt();
    	dpBaseDataDictionaryExt.setBaseWhere();
    	dpBaseDataDictionaryExt.withOutPage();
    	dpBaseDataDictionaryExt.setParentValue(DataDicType.FUND_ITEM.getCode());
    	dpBaseDataDictionaryExt.setAddedValue1("01"); //启用的明细
    	dpBaseDataDictionaryExt.setOrderSql("`VALUE`");
//		只展示附加值为1的数据
    	List<DpBaseDataDictionary> dpBaseDataDictionarys = dBaseDataDictionaryService.selectByExt(dpBaseDataDictionaryExt);
    	
    	DpBaseDataDictionaryExt depositItemExt = new DpBaseDataDictionaryExt();
    	depositItemExt.setBaseWhere();
    	depositItemExt.setRows(1);
    	depositItemExt.setParentValue(DataDicType.FUND_ITEM.getCode());
    	depositItemExt.setValue("35");
    	List<DpBaseDataDictionary> depositDataDictionaryItems = dBaseDataDictionaryService.selectByExt(depositItemExt);
    	if(CollectionUtils.isNotEmpty(depositDataDictionaryItems)) {
    		dpBaseDataDictionarys.add(depositDataDictionaryItems.get(0));
    	} else {
    		DpBaseDataDictionary dpBaseDataDictionary = new DpBaseDataDictionary();
    		dpBaseDataDictionary.setValue("35");
    		dpBaseDataDictionary.setName("征信押金");
    		dpBaseDataDictionary.setParentValue(DataDicType.FUND_ITEM.getCode());
    		dpBaseDataDictionarys.add(dpBaseDataDictionary);
    	}
    	depositItemExt.setBaseWhere();
    	depositItemExt.setRows(1);
    	depositItemExt.setParentValue(DataDicType.FUND_ITEM.getCode());
    	depositItemExt.setValue("01");
    	List<DpBaseDataDictionary> depositDataDictionaryItems_ = dBaseDataDictionaryService.selectByExt(depositItemExt);
    	if(CollectionUtils.isNotEmpty(depositDataDictionaryItems_)) {
    		dpBaseDataDictionarys.add(depositDataDictionaryItems_.get(0));
    	} else {
    		DpBaseDataDictionary dpBaseDataDictionary = new DpBaseDataDictionary();
    		dpBaseDataDictionary.setValue("01");
    		dpBaseDataDictionary.setName("按揭服务费");
    		dpBaseDataDictionary.setParentValue(DataDicType.FUND_ITEM.getCode());
    		dpBaseDataDictionarys.add(dpBaseDataDictionary);
    	}
//    	获取费用数据字典=====================
    	
    	
//    	==================应收获取============================
    	List<Map<String, Double>> fees = ywFeesSubService.countReceiveByBusiId(id, null);
//    	==================应收获取============================
    	
//    	==================已收获取============================   	
    	List<String> exludeKeys = Arrays.asList(new String[]{"01", "35"});
    	List<CwFundItem> fundItems = CollectionInitUtil.emptyList();
    	List<CwFundItem> fundItems_ = CollectionInitUtil.emptyList();
    	List<Map<String, Double>> remits = cwIncomeExpensesItemService.countReceiveByBusiId(id, null);
    	if(CollectionUtils.isNotEmpty(dpBaseDataDictionarys)) {
    		for(DpBaseDataDictionary dpBaseDataDictionary : dpBaseDataDictionarys) {
    			CwFundItem cwFundItem = new CwFundItem();
    			String key = dpBaseDataDictionary.getValue();
    			cwFundItem.setKey(key);
    			cwFundItem.setName(dpBaseDataDictionary.getName());
    			cwFundItem.setFeeCount(ViewScopeControllerHelper.getItem(fees, key));
    			cwFundItem.setRemitCount(ViewScopeControllerHelper.getItem(remits, key));
    			if(exludeKeys.contains(key)) {
    				fundItems_.add(cwFundItem);
    			} else {
    				fundItems.add(cwFundItem);
    			}
    		}
    	}
//    	==================已收获取============================  
    	model.addAttribute("fundItems", fundItems);
    	model.addAttribute("fundItems_", fundItems_);
    	
    	CwFundItem cwFundItem = new CwFundItem();
    	cwFundItem.setFeeCount(ViewScopeControllerHelper.countItems(fees, null));
    	cwFundItem.setRemitCount(ViewScopeControllerHelper.countItems(remits, null));
    	model.addAttribute("fundTotal", cwFundItem);
    	return "system/cw/shUpdatedaikuanBDetail";
    }
}
